package cn.yuanqiao.cqbc.chuku.service.impl;
/**
 * @author 赵常飞
 * @date 2024/6/24 19:18
 */



import cn.yuanqiao.activiti.domain.ActivitiPackage;
import cn.yuanqiao.activiti.service.RemoteActivitiService;
import cn.yuanqiao.common.core.context.ResponseUtils;
import cn.yuanqiao.common.core.domain.R;
import cn.yuanqiao.common.core.domain.TreeSelect;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.cqbc.chuku.mapper.ChukuMapper;
import cn.yuanqiao.cqbc.chuku.service.IChukuService;
import cn.yuanqiao.cqbc.common.mapper.MapperToolsMapper;
import cn.yuanqiao.cqbc.dataManage.domain.DataManage;
import cn.yuanqiao.cqbc.dataManage.mapper.DataManageMapper;
import cn.yuanqiao.cqbc.deleteIdentify.service.impl.DeleteIdentifyServiceImpl;
import cn.yuanqiao.feignService.RemoteCommonService;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *@Author: 赵常飞
 *@Date: 2024/6/24 19:18
 *@Description:
 *@Note:
 */
@Service
public class ChukuServiceImpl implements IChukuService {
    @Autowired
    private DeleteIdentifyServiceImpl deleteIdentifyService;
    @Autowired
    private RemoteCommonService remoteCommonService;
    @Autowired
    private MapperToolsMapper DbTool;
    @Autowired
    private ChukuMapper chukuMapper;
    @Autowired
    private RemoteActivitiService activitiService;
    @Autowired
    private DataManageMapper dataManageMapper;
    @Override
    public List<TreeSelect> getTreeList() {
        R<List<TreeSelect>> resultR=remoteCommonService.getdanganTree();
        return resultR.getData();
    }

    @Override
    public List<Map<String, Object>> getList(JSONObject json) {
        return deleteIdentifyService.getList(json);
    }

    @Override
    public int fqsp(JSONObject json) {
        String tenantCode= SecurityUtils.getLoginUser().getSysUser().getTenantCode();
        Long id=json.getLong("id");
        Long categoryId=json.getLong("categoryId");
        Map map=new HashMap(1<<1);
        map.put("id",id);
        map.put("categoryId",categoryId);
        Map<String, Object> variables = new HashMap();
        variables.put("processVariables", map);
        R<ActivitiPackage> activitiPackageR = activitiService.startProcessWorkFlowCQBCChuku(variables);
        String activitiId = activitiPackageR.getData().getProcessInstance().getId();
        //更新表数据的delete_state 0:审批中 1:审批结束
        DbTool.executeUpdate("UPDATE YQ_DATA_MANAGE set chuku_state=0 where id ="+id+";update "+tenantCode+"_CQBC_HDDA set chuku_state=0 where data_id="+id+";update "+tenantCode+"_CQBC_FOLDER set chuku_state=0 where data_id="+id+";update "+tenantCode+"_CQBC_ITEM set chuku_state=0 where data_id="+id+";");
        return 1;
    }

    @Override
    public int cqbcChukuBanjie(Map<String,Object> map) {
        String tenantCode= SecurityUtils.getLoginUser().getSysUser().getTenantCode();
        String id=String.valueOf(map.get("id"));
        String categoryId=String.valueOf(map.get("categoryId"));
        //更新表数据的delete_state 0:审批中 1:审批结束
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowdate=sdf.format(date);
        DbTool.executeUpdate("UPDATE YQ_DATA_MANAGE set chuku_state=1,ischuku=1,OUTBOUND_TIME='"+nowdate+"' where id ="+id+";update "+tenantCode+"_CQBC_HDDA set chuku_state=1,ischuku=1 where data_id="+id+";update "+tenantCode+"_CQBC_FOLDER set chuku_state=1,ischuku=1 where data_id="+id+";update "+tenantCode+"_CQBC_ITEM set chuku_state=1,ischuku=1 where data_id="+id+";");
        return 1;
    }

    @Override
    public void killcqbcChuku(Map<String,Object> map) {
        String tenantCode= SecurityUtils.getLoginUser().getSysUser().getTenantCode();
        String id=String.valueOf(map.get("id"));
        String categoryId=String.valueOf(map.get("categoryId"));
        //更新表数据的delete_state 0:审批中 1:审批结束
        DbTool.executeUpdate("UPDATE YQ_DATA_MANAGE set chuku_state = NULL where id ="+id+";update "+tenantCode+"_CQBC_HDDA set chuku_state = NULL where data_id="+id+";update "+tenantCode+"_CQBC_FOLDER set chuku_state = NULL where data_id="+id+";update "+tenantCode+"_CQBC_ITEM set chuku_state = NULL where data_id="+id+";");
    }

    @Override
    public void download(Long id, HttpServletResponse response) throws IOException {
        DataManage dataManagebyId = dataManageMapper.getDataManagebyId(id);
        String filePath=dataManagebyId.getPath()+ File.separator+dataManagebyId.getName()+"."+dataManagebyId.getFormat();
        byte[] bytes = toByteArray(filePath);
        String fileName = URLEncoder.encode(dataManagebyId.getName()+"."+dataManagebyId.getFormat(), "UTF-8");
        response.reset();
        ResponseUtils.defaultResponse(response);
        response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
        response.addHeader("Content-Length", "" + bytes.length);
        response.setContentType("application/octet-stream;charset=UTF-8");
        OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
        outputStream.write(bytes);
        outputStream.flush();
        outputStream.close();
        response.flushBuffer();
    }

    public static byte[] toByteArray(String filename) {
        File f = new File(filename);
        if (!f.exists()) {
            throw new ServiceException("文件不存在!");
        } else {
            FileChannel channel = null;
            FileInputStream fs = null;

            try {
                fs = new FileInputStream(f);
                channel = fs.getChannel();
                ByteBuffer byteBuffer = ByteBuffer.allocate((int)channel.size());

                while(channel.read(byteBuffer) > 0) {
                }

                byte[] var5 = byteBuffer.array();
                return var5;
            } catch (IOException var17) {
                throw new ServiceException("文件不存在!");
            } finally {
                try {
                    channel.close();
                } catch (IOException var16) {
                    throw new ServiceException("文件不存在!");
                }

                try {
                    fs.close();
                } catch (IOException var15) {
                    throw new ServiceException("文件不存在!");
                }
            }
        }
    }
}
